<template>
  <div :class="className" :style="{height:height,width:width}" />
</template>

<script>
import echarts from 'echarts'
require('echarts/theme/macarons') // echarts theme
import { debounce } from '@/utils'
import cabinetLog from '@/api/order/cabinetLog.js'

const animationDuration = 6000

export default {
  props: {
    className: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '300px'
    }
    // userList: {
    //   type: Array,
    //   default: []
    // }
  },
  data() {
    return {
      chart: null,
      userList: [],
      userTime: []
    }
  },
  mounted() {
    this.initChart()
    this.__resizeHandler = debounce(() => {
      if (this.chart) {
        this.chart.resize()
      }
    }, 100)
    window.addEventListener('resize', this.__resizeHandler)
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    window.removeEventListener('resize', this.__resizeHandler)
    this.chart.dispose()
    this.chart = null
  },
  methods: {
    initChart() {
      cabinetLog.get({
        page : 0,
        size : 30,
        sort: 'id,desc'
      }).then(res => {
        this.userList = [];
        this.userTime = [];
        res.content.forEach(item => {
          var userTime = item.status ? 0 : parseInt(((( new Date(item.endTime) - new Date(item.startTime) ).valueOf()) % (1000 * 60 * 60)) / (1000 * 60));
          var cabinetName = item.cabinetName + item.qrCode.name;
          this.userTime.push(userTime);
          this.userList.push(cabinetName);
        })
        this.chart = echarts.init(this.$el, 'macarons')
        var a = res.content;
        this.chart.setOption({
          title: {
            text: '充电仓使用概况',
            subtext: '次/分钟'
          },
          grid:{          //显示数据的图表位于当前canvas的坐标轴
            x:85,
            y:40,
            x2:85,
            y2:40,
            borderWidth:1
          },
          tooltip: {
            trigger: 'axis'
          },
          legend: {},
          toolbox: {
            show: true,
            feature: {
              dataZoom: {
                yAxisIndex: 'none'
              },
              dataView: { readOnly: false },
              magicType: { type: ['line', 'bar'] },
              restore: {},
              saveAsImage: {}
            }
          },
          xAxis: {
            type: 'category',

            labels: {
              enabled: true,
            },
            boundaryGap: false,
            data: this.userList
          },
          yAxis: {
            type: 'value',
            axisLabel: {
              formatter: '{value}'
            },
            max: function(value) {
              return value.max + 6;
            }
          },
          series: [
            {
              name: '使用时长',
              type: 'line',
              data: this.userTime,
              markPoint: {
                data: [
                  { type: 'max', name: 'Max' },
                  { type: 'min', name: 'Min' }
                ]
              },
              markLine: {
                data: [{ type: 'average', name: 'Avg' }]
              }
            }
          ]
        })
      })
    }
  }
}
</script>
